/** Project : Sticky Notes Plus */
/** Author  : Sainagakishore Srikantham */
/** Email   : ssn.kishore@gmail.com */

/** File    : StickyNote.cpp */
/** Purpose : This file contains the definition for the class CStickyNote which is basic type of note the user
              can create. This class also is the base for other types of notes. */

#include <QTextEdit>
#include <QVBoxLayout>
#include <QMessageBox>
#include <string>
#include <QString>
#include <QLabel>

#include "StickyNote.h"

using std::string;

const int CStickyNote::iNoteLength = 140;

/** Constructor of the class */
CStickyNote::CStickyNote(int aNoteId, bool aBeingReloaded)
{
    QString temp;
    temp.setNum(aNoteId);

    iTitle = "Untitled Note ";
    iTitle.append(temp);
    setWindowTitle(iTitle);

    iNoteId = aNoteId;
    iNoteType = STICKY_GENERIC_NOTE;

    iTextEdit = new QTextEdit();
    connect(iTextEdit, SIGNAL(textChanged()), this, SLOT(CheckNoteWidth()));

    iCharsRem = new QLabel(tr("140 Character(s) Remaining"));

    iMainLayout = new QVBoxLayout();

    iMainLayout->addWidget(iTextEdit);
    iMainLayout->addWidget(iCharsRem);

    setLayout(iMainLayout);

    if(!aBeingReloaded)
        show();
}

/** Destructor of the class */
CStickyNote::~CStickyNote()
{
    if(iTextEdit)
    {
        delete iTextEdit;
        iTextEdit = NULL;
    }

    if(iMainLayout)
    {
        delete iMainLayout;
        iMainLayout = NULL;
    }

    if(iCharsRem)
    {
        delete iCharsRem;
        iCharsRem = NULL;
    }
}

/** Function called on the "text modified" signal to check that the user doesn't surpass 140 characters */
void CStickyNote::CheckNoteWidth()
{
    static int allowedLength = iNoteLength;

    QString currentText = GetMsg();
    string stringText = currentText.toStdString();

    int strLen = stringText.length();
    int charsRem = (iNoteLength - strLen); /** -1 because the actual limit is 140 chars only
                                                but we need one extra for the backslash n character */

    if(charsRem >= 0)
    {
        QString tempStr;

        tempStr.setNum(charsRem);
        tempStr.append(" Character(s) Remaining");
        iCharsRem->setText(tempStr);
    }

    if( strLen > allowedLength)
    {
        allowedLength = iNoteLength + 1;
        stringText = stringText.substr(0, iNoteLength-1); /** Trimming back to 140 characters */
        currentText = stringText.c_str();
        currentText.append("\n");
        SetMsg(currentText);
    }
}

/** Returns the title of the note */
QString CStickyNote::GetTitle()
{
    return iTitle;
}

/** Sets the note title */
void CStickyNote::SetTitle(QString aTitle)
{
    iTitle = aTitle;
}

/** Returns the message of the note */
QString CStickyNote::GetMsg()
{
    iMsg = iTextEdit->toPlainText();
    return iMsg;
}

/** Sets the note message */
void CStickyNote::SetMsg(QString aMsg)
{
    iMsg = aMsg;
    iTextEdit->setText(iMsg);
}

/** Returns the type of the note */
NoteType CStickyNote::GetNoteType()
{
    return iNoteType;
}

/** Sets the note type */
void CStickyNote::SetNoteType(NoteType aNoteType)
{
    iNoteType = aNoteType;
}

/** Returns the note id */
int CStickyNote::GetNoteId()
{
    return iNoteId;
}
